package cn.jhc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import cn.jhc.bean.Goods;
import cn.jhc.bean.ShopCar;
import cn.jhc.bean.User;
import cn.jhc.db.GoodsDao;
import cn.jhc.db.ShopCarDao;
import cn.jhc.db.UserDao;
import cn.jhc.utils.Constants;

/**
 * Servlet implementation class AddToCar
 */
@WebServlet("/addtocar.do")
public class AddToCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToCarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		

	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DataSource dataSource = (DataSource)request.getServletContext().getAttribute(Constants.H2_DATASOURCE);
		String isbn = request.getParameter("isbn");
		List<Goods>goods;
		GoodsDao goodsDao = new GoodsDao(dataSource);
		 goods =  goodsDao.selectbyisbn(isbn);
		String goodname = goods.get(0).getGoodname();
	    String price = goods.get(0).getPrice();
		String discount = goods.get(0).getDiscount();
		String img = goods.get(0).getImg();
		
		
		ShopCarDao shopCarDao = new ShopCarDao(dataSource);
		ShopCar shopCar = new ShopCar();
		shopCar.setGoodname(goodname);
		shopCar.setGoodbn(isbn);
		shopCar.setPrice(price);
		shopCar.setDisount(discount);
		shopCar.setImg(img);
		
		System.out.println(price);
		System.out.println(discount);
		shopCar.setQuantity("");
		shopCar.setTotal("");
		try {
//			shopCarDao.DropCar(shopCar);
			shopCarDao.addCar(shopCar);
			System.out.println(shopCar.getGoodname());
			System.out.println(shopCar.getImg());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.getSession().setAttribute("filterisbn", isbn);
		 System.out.println(isbn);
	}
}
